package com.huahang.fashionpress.controller;

import com.huahang.fashionpress.form.FormImg;
import com.huahang.fashionpress.mapper.ImgMapper;
import com.huahang.fashionpress.model.Img;
import com.huahang.fashionpress.model.Product;
import com.huahang.fashionpress.service.ImgService;
import com.huahang.fashionpress.service.ProductService;
import com.huahang.fashionpress.util.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "img")
public class AdminImgController{
    @Resource
    ImgService imgService;
    @Resource
    ProductService productService;
    @Resource
    ImgMapper imgMapper;
    //查找所有图片
    @RequestMapping(value = "/list")
    public ModelAndView LinkImg(Map<String,Object> map){
        List<FormImg> imgList = imgService.SelectAllImg();
        List<String> products =imgService.SelectAllproductId();
        map.put("imgList",imgList);
        map.put("products",products);
        return  new ModelAndView("admin/img/list",map);
    }
    //根据商品名称查图片
    @RequestMapping(value = "/selectByproductName/{productName}")
    public ModelAndView SelectimgByproductName(Map<String,Object> map ,@PathVariable String productName){
        List<Product> productList = productService.selectProductByName(productName);
        List<String> products = new ArrayList<>();
        List<FormImg> imgList = new ArrayList<>();
        for (Product product :
                productList) {
            imgList = imgService.SelectFImgByproduct_id(product.getProductId());
            products = new ArrayList<>() ;
            products.add(product.getProductId());
        }

        map.put("products",products);
        map.put("imgList",imgList);
        return new ModelAndView("admin/img/list",map);
    }
    //根据商品ID查图片
    @RequestMapping(value = "/selectByproductId/{productId}")
    public ModelAndView SelectimgByproductId(Map<String,Object> map ,@PathVariable String productId){
        List<FormImg> imgList = imgService.SelectFImgByproduct_id(productId);
        List<String> products = new ArrayList<>();
        products.add(productId);
        map.put("imgList",imgList);
        map.put("products",products);
        return new ModelAndView("admin/img/list",map);
    }
    //    @RequestMapping(value = "/add")
//    public String linkaddImg()
//    {
//        return "admin/img/add";
//    }
    //传递需要修改的图片信息
    @RequestMapping(value="/add",method = RequestMethod.GET)
    public ModelAndView updateImg(int img_id,String imgStatus,Map<String,Object> map){
        FormImg formImg = imgService.SelectFImgByimg_id(img_id);
        map.put("formimg",formImg);
        map.put("imgStauts",imgStatus);
        return new ModelAndView("admin/img/add",map);
    }
    //添加图片信息
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String updateImg(String imgStatus,MultipartFile file,String productId,Model model, HttpServletRequest request){
        String filename = file.getName();
        if(imgStatus=="展示"){
            Img img = imgMapper.SelectPrImgByproduct_id(productId);
            if(img!=null){
                imgService.updateImgStatus(img);
            }
            FileUtils.uploadFile(file,productId,1,imgMapper);
        }
        FileUtils.uploadFileList(productId,0,imgMapper,filename,request);
        model.addAttribute("url", "/fashionpress/img/list");
        return "admin/common/success";
    }
    //更改商品的展示图片
    @RequestMapping(value = "/statu")
    public String updateStatu(int img_id,Model model){
        Img img = imgService.SelectImgByimg_id(img_id);
        imgService.updateImgStatus(img);
        model.addAttribute("msg", "更改展示图片成功！");
        model.addAttribute("url", "/fashionpress/img/list");
        return "admin/common/success";
    }
    @RequestMapping(value = "/delete")
    public String deleteimg(int img_id,String product_id, Model model){
        imgService.deleteImgByimg_id(img_id,product_id);
        model.addAttribute("msg", "删除图片成功！");
        model.addAttribute("url", "/fashionpress/img/list");
        return "admin/common/success";
    }
}
